local ROT = require 'rot'
local RNG = ROT.RNG

local Scene = require 'game.scene'

local SceneSpec = Scene:extend()

SceneSpec.name = 'Volcanic Cave'

SceneSpec.innerWidth = 25
SceneSpec.innerHeight = 25
SceneSpec.fovRadius = 4
SceneSpec.maxDepth = 4

function SceneSpec:build(depth, x1, y1, x2, y2)
    local m

    if depth > 0 then

        self.name = 'Volcanic Cave ' .. tostring(depth)
        self.ambientLight = { 200, 120, 80 }
        self.innerWidth, self.innerHeight = 5 * depth + 25, 5 * depth + 25

        local max = math.max(4, depth + 3)
        local min = math.max(4, max - 4)
        local dim = { min, max }

        m = ROT.Map.Uniform:new(self.innerWidth, self.innerHeight, {
            roomDugPercentage = depth / (self.maxDepth + 1),
            roomWidth = dim,
            roomHeight = dim,
        })
        m:create(function (x, y, val)
            self.feature:setCell(x, y, val == 0 and 'obsidian floor' or nil)
        end)


        local rooms = m:getRooms()
        local i = #rooms
        if i >= 1 then
            local r = rooms[1]
            require 'scene.forest' .build(self, 0,
                r:getLeft(), r:getTop(), r:getRight(), r:getBottom())
        end

        self:expand('obsidian wall')

    else
        local x, y = math.floor((x1 + x2) * 0.5), math.floor((y1 + y2) * 0.5)

        for _x = x1, x2 do for _y = y1, y2 do
            self.feature:setCell(_x, _y, 'obsidian wall')
        end end

        self.feature:setCell(x, y, 'obsidian floor')
        self:decimate('obsidian wall', 7, 1, 'obsidian floor')
        -- self:decimate('obsidian wall', 6, 1, 'floor')
        -- for _x = x-1, x+1 do for _y = y-1, y+1 do self.feature:setCell(_x, _y, 'floor') end end
        -- for _x = x - 3, x + 3 do self.feature:setCell(_x, y, 'floor') end
        -- for _y = y - 3, y + 3 do self.feature:setCell(x, _y, 'floor') end

        self.branches[#self.branches + 1] = { x = x, y = y, branch = 'hell' }
        self.feature:setCell(x, y, 'downstairs')
    end

    -- [[
    self:decimate('obsidian wall', 3, 1, 'lava')
    self:decimate('obsidian wall', 7, 1, 'obsidian floor')
    self:decimate('obsidian wall', 7, 1, 'lava')
    self:decimate('obsidian wall', 7, 1, 'obsidian floor')
    --]]

    for i = 1, math.min(3, math.max(1, depth)) do
    -- self:decimate('wall', 3, 1, 'lava')
    self:decimate('obsidian wall', 7, 1, 'lava')
    self:decimate('obsidian wall', 7, 1, 'obsidian floor')
    self:decimate('obsidian wall', 6, 1, 'obsidian floor')
    self:decimate('obsidian wall', 2, 1, 'obsidian floor')
    end

    self:decimate('obsidian wall', 5, 1, 'obsidian floor', function(self, x, y)
        if RNG:random() <= 0.1 then
            local mob = self:spawn(x, y, 'gorilla')
            if RNG:random() <= 0.5 then self:equip(mob, 'hide') end
            if RNG:random() <= 0.5 then self:equip(mob, 'stick') end
        elseif RNG:random() <= 0.1 then
            local mob = self:spawn(x, y, 'cheetah')
        end

    end)

    if depth < 1 then
        self.builder:erode('obsidian wall')
        self.builder:erode('obsidian wall')
        return
    end

    local rooms = m:getRooms()

    local offset = depth == 1 and 1 or 3

    for i = 1, offset do
        local c = rooms[i]:getCenter()
        local x, y = RNG:random(-1, 1) + c[1], RNG:random(-1, 1) + c[2]
        self.upstairs[i] = { x = x, y = y }
        self.feature:setCell(x, y, 'upstairs')
        self.player.x, self.player.y = x, y
    end

    if depth < self.maxDepth then
        for i = offset + 1, offset + 3 do
            local c = (rooms[i] or rooms[#rooms]):getCenter()
            local x, y = RNG:random(-1, 1) + c[1], RNG:random(-1, 1) + c[2]
            self.downstairs[i - offset] = { x = x, y = y }
            self.feature:setCell(x, y, 'downstairs')
        end
    end

    --[[
    self:decimate('floor', 8, 1, 'xwall')
    for i = 1, 4 do
        self:decimate('xwall', 4, 1, 'floor')
        self:decimate('xwall', 3, 1, 'floor')
    end

    for i, x, y, v in self.feature:each() do
        if v == 'xwall' then self.feature:setCell(x, y, 'wall') end
    end
    --]]

     -- [[
    self:decimate('obsidian floor', 8, 1, 'shallow water')
    self:decimate('shallow water', 3, 1, 'obsidian floor')
    self:decimate('shallow water', 1, 1, 'obsidian floor')
    self:decimate('shallow water', 8, 1, 'deep water')
    self:decimate('deep water', 3, 1, 'shallow water')
     --]]

end

return SceneSpec
